from datetime import time
from flask import Flask,request,jsonify,Response
import json

from flask import Blueprint
from flask.helpers import make_response
from databases import db

import time
news = Blueprint('new',__name__)







@news.route('/new/',methods=['POST'])
def new():
    data = json.loads(request.data)
    print(data)
    new = data['new']
   
    sql = "insert into `title`(new) values ('%s')" % new
    db.insert(sql)
    return {"code":200}



# 钩子函数 执行后处理
@news.before_request
def before_request():
    #获取当前url
    url = request.url
    # 获取时间戳
    start_time = time.time()
    sql = "insert into url_end(url,start_time) values ('%s',%d)" %(url,start_time)
    db.insert(sql)
    return {"code":200}



# # 钩子函数 执行后处理
# @news.after_request
# def after_request(response):
#     #获取当前url
#     url = request.url
#     # 获取时间戳
#     end_time = time.time()
    # 根据url查询
#     print(url,end_time)
#     sql = " select * from url_end where url='%s' and end_time is Null" % (url)
#     res = db.get_one(sql)
#     sql = "update `url_end` set end_time='%s'" %(end_time)
#     db.update(sql)

#     #结束更新时间
#     return make_response(response)


